package cn.dengta.webapp.user.entity;

import java.math.BigDecimal;
import java.util.Date;

import cn.dengta.common.model.State;
import cn.dengta.common.model.Status;
import cn.dengta.common.util.Formats;
import cn.dengta.webapp.base.entity.BizEntity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 获奖记录
 *
 * @author lgy
 * @since 2022-08-10
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName("dt_prize_log")
public class PrizeLog extends BizEntity {

	/** 名称 */
	protected String name;
	/** 图片 */
	protected String icon;
	/** 数量 */
	protected Integer count;
	/** 奖品价值 */
	protected Integer amount;
	/** 用户ID */
	private Long userId;
	/** 活动ID */
	private Long activityId;
	/** 奖品ID */
	private Long prizeId;
	/** 奖品发放人 */
	private Long sendUserId;
	/** 发货时间 */
	private Date sendTime;
	/** 交易额度 */
	protected BigDecimal tradeAmount;
	/** 本次交易使用的免费钻石 */
	protected BigDecimal tradeFreePart;
	/** 排序 */
	private Integer sort;
	/** 备注 */
	private String remark;

	public PrizeLog init(String name, String icon, Integer count, Integer amount, Long userId, Long activityId, Long prizeId, BigDecimal tradeAmount, BigDecimal tradeFreePart, Integer status, Date now) {
		this.name = name;
		this.icon = StringUtil.toString(icon);
		this.count = X.expectNotNull(count, 1);
		this.amount = amount;
		this.userId = userId;
		this.activityId = activityId;
		this.prizeId = prizeId;
		this.sendTime = null;
		this.tradeAmount = tradeAmount.add(tradeFreePart);
		this.tradeFreePart = tradeFreePart;
		this.sort = 0;
		this.remark = "";
		this.initTime(now);
		if (Status.asResult(status) && sendUserId == null) {
			this.sendTime = now;
			this.sendUserId = 1L;
		}
		super.setStatusAndState(status, State.PUBLIC);
		return this;
	}

	public PrizeLog init(PrizeLog input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.activityId = X.expectNotNull(activityId, input.activityId);
		this.prizeId = X.expectNotNull(prizeId, input.prizeId);
		this.sort = X.expectNotNull(input.sort, sort);
		super.initTime(now);
		super.init(input.status, input.state);
		return this;
	}

	public String getSendTime_() {
		return Formats.formatDate_D(sendTime);
	}

	public boolean canSend() {
		return Status.NO.eq(status);
	}

	public static final String USER_ID = "user_id";
	public static final String ACTIVITY_ID = "activity_id";
	public static final String PRIZE_ID = "prize_id";
	public static final String SEND_USER_ID = "send_user_id";
	public static final String NAME = "name";
	public static final String ICON = "icon";
	public static final String COUNT = "count";
	public static final String AMOUNT = "amount";
	public static final String TRADE_AMOUNT = "trade_amount";
	public static final String TRADE_FREE_PART = "trade_free_part";
	public static final String SEND_TIME = "send_time";
	public static final String REMARK = "remark";
	public static final String SORT = "sort";

}
